<?php

  class user {

    /**
    * Create access token was sets   
    *
    * @param string $access_token an access token.
    * 
    */
    public static function createAccessToken($apikey, $secretkey) {  

      global $db;  

      if(isset($_SESSION['user'])){  

        if( empty($_SESSION['user']['id']) ){
          $userinfo = self::getUser();  
          $_SESSION['user']['id'] = $userinfo['userid'];   
          $_SESSION['user']['lastactive'] = $userinfo['lastactive'];
          $_SESSION['user']['createdate'] = $userinfo['createdate'];
          unset($userinfo);

          $charid = self::getCharinfo();  
          $_SESSION['user']['char'] = $charid; 
          unset($charid);  
        }  

        $_SESSION['user']['token'] = base64_encode( md5 ( microtime ( true ) . md5 ( $apikey .  $secretkey ) . md5 ( $_SESSION['user']['id'] ) . md5 ( microtime ( true ) ) ));      
        return true;

      } else { 

        return false;  

      }  
    }

    public static function getUser($usersource = 'facebook'){
      global $db, $memcache;

      $_SESSION['user']['fid']  = addslashes( $_SESSION['user']['fid'] );
      $_SESSION['user']['femail'] = addslashes( $_SESSION['user']['femail'] );

      // cache 
      if( $memcache->get( md5 ( $_SESSION['user']['fid'] ) ) ){
        return $memcache->get( md5 ( $_SESSION['user']['fid'] ) );
      }

      $usersource = addslashes( $usersource );

      $sql = 'SELECT * FROM `user` WHERE 
      `username` = \'%s\' OR 
      `useremail` = \'%s\' AND
      `usersource` = \'%s\' ; ';

      $sql = sprintf($sql, $_SESSION['user']['fid'], $_SESSION['user']['femail'], $usersource);
      $user = $db->fetch_array( $db->query($sql) );   

      if(empty($user)){
        $sql = 'INSERT INTO `user`(`username`, `useremail`, `usersource`, `isactive`, `lastactive`, `createdate`) 
        VALUES (\'%s\', \'%s\', \'%s\', 0, '.time().', '.time().');';
        $sql = sprintf($sql, $_SESSION['user']['fid'], $_SESSION['user']['femail'], $usersource);   
        if(!$db->query($sql)) {
          return false;
        } else {
          // cache
          $memcache->set( md5 ( $_SESSION['user']['fid'] ),   self::getUser($usersource) );
          return $memcache->get( md5 ( $_SESSION['user']['fid'] ) );  
        } 
      } else {

        $sql = 'UPDATE `user` SET `lastactive` = \'%s\' WHERE `userid` = \'%s\'; ';
        $sql = sprintf($sql, time(), $user['userid']); 
        $db->query($sql);   

        return $user;
      } 
    } 

    public static function getCharinfo(){
      global $db;

      $_SESSION['user']['id']  = addslashes( $_SESSION['user']['id'] );       

      $sql = 'SELECT * FROM `char` WHERE 
      `userid` = \'%s\'';

      $sql = sprintf($sql, $_SESSION['user']['id'], $_SESSION['user']['femail']); 
      $char = $db->fetch_array( $db->query($sql) );   

      if(empty($char)){
        $sql = 'INSERT INTO `char`(`userid`, `charname`)
        VALUES (\'%s\', \'%s\');';
        $sql = sprintf($sql, $_SESSION['user']['id'], $_SESSION['user']['fname']);   
        if(!$db->query($sql)) {
          return false;
        } else {
          return self::getCharinfo();
        } 
      } else {     
        return $char;
      } 
    } 

  }
?>
